$(document).ready(function() {
    searchBook(1);
});
$("#searchBook").click(function(){
    searchBook(1);
})

$("#pageSize").change(function(){
    searchBook(1);
})

function searchBook(pageNum){

    $("tbody").empty();

    let trHead = $("<tr></tr>");
    trHead.append("<td>"+"书id"+"</td>");
    trHead.append("<td>"+"书名"+"</td>");
    trHead.append("<td>"+"作者"+"</td>");
    trHead.append("<td>"+"书类型"+"</td>");
    trHead.append("<td>"+"价格"+"</td>");
    trHead.append("<td>"+"出版日期"+"</td>");
    trHead.append("<td>"+"封面"+"</td>");
    trHead.append("<td>"+"删除"+"</td>");
    trHead.append("<td>"+"修改"+"</td>");
    $("tbody").append(trHead);

    let bookName = $("#bookName").val();
    let writerName = $("#writerName").val();
    let bookType = $("#bookType").val();
    let startDate = $("#startDate").val();
    let endDate = $("#endDate").val();
    let pageSize = $("#pageSize").val();
    $.ajax({
        url:"books",
        type:"get",
        data:{
            "bookName":bookName,
            "writerName":writerName,
            "bookType":bookType,
            "startDate":startDate,
            "endDate":endDate,
            "pageNum":pageNum,
            "pageSize":pageSize
        },
        dataType:"json",
        success:function(data){
            console.log(data);
            let books = data.list;
            console.log(books);
            for(let i = 0;i<books.length;i++){
                let book = books[i];
                let tr = $("<tr></tr>");
                tr.append("<td>"+book.bookId+"</td>");
                tr.append("<td>"+book.bookName+"</td>");
                tr.append("<td>"+book.writerName+"</td>");
                tr.append("<td>"+book.bookType+"</td>");
                tr.append("<td>"+book.bookPrice+"</td>");
                tr.append("<td>"+book.pubDate+"</td>");
                tr.append("<td><img src='" + book.bookCover + "' class='bookImg'></td>");

                // 添加删除功能
                let deleteTd = $("<td></td>");
                let deleteForm = $("<form action='book/" + book.bookId + "' method='post'></form>");
                deleteForm.append("<input type='hidden' name='_method' value='delete'>");
                deleteForm.append("<input type='submit' value='删除' class='deleteBook'>");
                deleteTd.append(deleteForm);
                tr.append(deleteTd);

                // 添加修改功能
                let updateTd = $("<td></td>");
                let updateLink = $("<a href='update.jsp?bookId=" + book.bookId + "&bookName=" + encodeURIComponent(book.bookName) + "&bookType=" + encodeURIComponent(book.bookType) + "&writerName=" + encodeURIComponent(book.writerName) + "&pubDate=" + book.pubDate + "'>修改</a>");
                updateTd.append(updateLink);
                tr.append(updateTd);


                $("tbody").append(tr);
            }
            makePageBar(data);
        }
    })
}
function makePageBar(data) {
    let pageBar = $("#pageBar");
    pageBar.empty();
    if (data.hasPreviousPage) {
        let lastPage = $(`<a href="#" onclick="searchBook(${data.pageNum - 1})">上一页</a>`);
        pageBar.append(lastPage);
    }

    for (let i = 0; i < data.navigatepageNums.length; i++) {
        let a = $(`<a href="#" onclick="searchBook(${data.navigatepageNums[i]})">${data.navigatepageNums[i]}</a>`);
        if (data.pageNum == data.navigatepageNums[i]) {
            a.addClass("red");
        }
        pageBar.append(a);
    }
    if (data.hasNextPage) {
        let nextPage = $(`<a href="#" onclick="searchBook(${data.pageNum + 1})">下一页</a>`);
        pageBar.append(nextPage);
    }
}